import { defineStore } from 'pinia';
import _ from 'lodash';
import { queryList, markList } from '@/utils/commonList';
import type { ToolBarState, NidusItem } from './type';

const useToolBar = defineStore('ToolBar', {
  state: (): ToolBarState => ({
    currentItem: 0,
    queryToolList: [],
    markToolList: [],
    lesionList: []
  }),
  actions: {
    // 设置当前选中id
    setCurrentId(id: number, type: boolean = false) {
      this.currentItem = id;
      if (type) {
        // 为true是标注工具
        return this.markToolList.filter(item => item.id === id)[0];
      } else {
        // 为false是查看工具
        return this.queryToolList.filter(item => item.id === id)[0];
      }
    },
    // 设置当前病灶集合
    setCurrentLesionList(dataList: NidusItem[]) {
      this.lesionList = dataList;
    },
    // 派发当前state数据
    setDataList(partial: Partial<ToolBarState>) {
      this.$patch(partial);
    },
    // 根据类型获取过滤数据
    getToolList(type: string) {
      // 'pic' | 'mpr' | 'mp4'
      const result: ToolBarState = {
        currentItem: 0,
        queryToolList: [],
        markToolList: [],
        lesionList: []
      }
      const includes = ['pic', 'mpr', 'mp4'].includes(type);
      if (includes) {
        result.queryToolList = queryList.filter(item => item.type.includes(type));
        result.markToolList = markList.filter(item => item.type.includes(type));
      } else {
        result.queryToolList = _.cloneDeep(queryList);
        result.markToolList = _.cloneDeep(markList);
        // Promise.reject(new TypeError('传参错误'));
      }
      this.setDataList(result);
    },
  }
})

export default useToolBar;